package app.web.cartoon.report.word.tab;

import java.util.HashMap;
import java.util.Map;

import app.web.spe.CartoonSPE;
import cn.lily.com.component.widget.TElement;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.report.base.data.mark.EMarkLine;
import cn.lily.com.report.base.data.series.ESeries;
import cn.lily.com.report.base.entity.EToolBox;
import cn.lily.com.report.base.entity.EToolTip;
import cn.lily.com.report.base.entity.axis.type.EAxisCategory;
import cn.lily.com.report.base.entity.toolbox.EMagicType;
import cn.lily.com.report.com.bar.EBarCharts;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.page.TPanel;
/**
 * 动漫首字母统计
 * @author JiaJ
 * @date 2017年12月7日
 *
 */
public class WordTotalReport extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TPanel cartoonWordPanel = Panel("cartoonWordPanel");
		cartoonWordPanel.creatToolbar().setBarText("【"+BusinessDate.getToday()+"】动漫首字母统计");
		
		//无关闭按钮
		cartoonWordPanel.setClose(false);
		cartoonWordPanel.createPanelCell(this.instCharts());
		
		getPage().addElement(cartoonWordPanel);
		
	}

	private TElement instCharts() {
		
		//柱形图对象
		EBarCharts barCharts = new EBarCharts("cartoonFanBar");
		
		//悬浮提示对象 - 线状悬浮
		barCharts.createToolTip(EToolTip.TRIGGER_AXIS);
		//图例
		barCharts.createLegend("基本数,详细数");
		
		//工具箱  -------------------------------------
		EToolBox toolBox = barCharts.createToolBox();
		//辅助箱工具
		toolBox.createMark();
		//数据视图工具,设置只读属性false
		toolBox.createDataView().setReadOnly(false);
		//图表转换工具
		toolBox.createMagicType()
			   .addType(EMagicType.EMAGIC_BAR)      //柱形图
			   .addType(EMagicType.EMAGIC_LINE);    //折线图
		//刷新工具
		toolBox.createRestore();
		//图片保存工具
		toolBox.createSaveImage();
		
		//创建拖拽对象
		barCharts.createCalculable();
		
		//创建X轴对象 - 创建对象X轴
		EAxisCategory xCategory = barCharts.createXAxis().createAxisCategory();
		//添加X轴数据集
		xCategory.addEasyDatas("#,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
		
		//创建Y轴对象 - 创建数据Y轴
		barCharts.createYAxis().createAxisValue();
		
		//数据集对象
		ESeries series = barCharts.createSeries();
		
		//填入数据
		createData(series);
		
		return barCharts;
	}
	
	private void createData(ESeries series) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		String sql = "select * from LEO_CAN_REPORT_WORD ";
		DataTable data = DBFactory.getActionDB().executeQueryTable(sql);
		
		Map<String,Integer> totalMap = new HashMap<String, Integer>();
		Map<String,Integer> detailMap = new HashMap<String, Integer>();
		
		while(data.next()){		
			totalMap.put(data.getString("CAN_FIRST"), data.getInt("CAN_BASE_NUM"));
			detailMap.put(data.getString("CAN_FIRST"), data.getInt("CAN_DETAIL_NUM"));		
		}
		
		String allWord = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuffer totalWord = new StringBuffer();
		StringBuffer detailWord = new StringBuffer();
		int wordLength = allWord.length();
		for(int i=0;i<wordLength;i++){
			String active = allWord.charAt(i)+"";
			
			if(totalMap.containsKey(active)){				
				totalWord.append(","+totalMap.get(active));
				detailWord.append(","+detailMap.get(active));			
			}else{			
				totalWord.append(",0");
				detailWord.append(",0");				
			}
			
		}
		
		//第一条记录
		ESeries series1 = series.createESeries();
		series1.setName("基本数");
		series1.setType(ESeries.SERIES_TYPE_BAR);
		series1.createData().addEasyDatas(totalWord.toString().substring(1));

		//标记线
		EMarkLine markLine1 = series1.createMarkLine();
		markLine1.createData().setName("平均值").setType("average");
		
		//第二条记录
		ESeries series2 = series.createESeries();
		series2.setName("详细数");
		series2.setType(ESeries.SERIES_TYPE_BAR);
		series2.createData().addEasyDatas(detailWord.toString().substring(1));
		
		EMarkLine markLine2 = series2.createMarkLine();
		markLine2.createData().setName("平均值").setType("average");

	}

}
